package com.syxp.pda.action.standard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.DefineIDZone;
import com.syxp.pda.entity.base.DeviceBaseInfo;
import com.syxp.pda.entity.base.Profess;
import com.syxp.pda.entity.standard.DeviceCheckStandard;
import com.syxp.pda.service.base.ProfessService;
import com.syxp.pda.service.standard.DeviceCheckStandardService;

public class DeviceCheckStandardAction extends BaseAction {
	@Resource
	private DeviceCheckStandardService deviceCheckStandardService;
	
	private int deviceInfoID;
	private int chkStandardID;
	private DeviceCheckStandard deviceCheckStandard;
	
	private int nodeDepth;
	private String nodeId;
	@Resource
	private ProfessService professService;
	
	private String isChk;
	
	public String loadtree() {
		System.out.println(this.nodeDepth+"###"+this.nodeId);
		List list = new ArrayList(); 
		try {
			int id = Integer.parseInt(this.nodeId.split("#")[this.nodeDepth]);
			if(nodeDepth==0) {//点击根节点  获取专业
				List<Profess> professes = this.professService.getDao().getAll();
				for(Profess o: professes) {
					Map map = new HashMap();
					map.put("id", this.nodeId+"#"+o.getProfessID());
					map.put("text", o.getProfessName());
					list.add(map);
				}
			} else if(this.nodeDepth==1) {//点击第二层专业节点  获取区域
				List<DefineIDZone> zones = this.deviceCheckStandardService.getZones(id);
				for(DefineIDZone o: zones) {
					Map map = new HashMap();
					map.put("id", this.nodeId+"#"+o.getZoneID());
					map.put("text", o.getZoneName());
					list.add(map);
				}
			} else if(this.nodeDepth==2) {//点击第三层区域节点  获取设备
				List<DeviceBaseInfo> devices = this.deviceCheckStandardService.getDevices(id);
				for(DeviceBaseInfo o: devices) {
					Map map = new HashMap();
					map.put("id", this.nodeId+"#"+o.getDeviceInfoID());
					map.put("text", o.getDeviceName());
					map.put("leaf", true);
					list.add(map);
				}
			} else if(this.nodeDepth==3) {//点击第四层设备节点  获取巡检项目
				List<DeviceCheckStandard> deviceCheckStandards = this.deviceCheckStandardService.getDeviceCheckStandard(id);
				for(DeviceCheckStandard o: deviceCheckStandards) {
					Map map = new HashMap();
					map.put("chkStandardID", o.getChkStandardID());
					map.put("chkPlace", o.getChkPlace());
					map.put("chkItem", o.getChkItem());
					map.put("chkContent", o.getChkContent());
					map.put("standard", o.getStandard());
					map.put("alCode", o.getAlCode());
					map.put("normalValue", o.getNormalValue());
					map.put("numberUnit", o.getNumberUnit());
					map.put("alarmTop", o.getAlarmTop());
					map.put("alarmBottom", o.getAlarmBottom());
					map.put("quantifyTop", o.getQuantifyTop());
					map.put("quantifyBottom", o.getQuantifyBottom());
					map.put("IsCHK", o.getIsCHK()?"是":"否");
					map.put("remarks", o.getRemarks());
					map.put("runCHKState", o.getRunCHKState());
					
					map.put("deviceInfoID", o.getDevice().getDeviceInfoID());
					map.put("deviceName", o.getDevice().getDeviceName());
					
					map.put("deviceRunStateCode", o.getDeviceRunState().getDeviceRunStateCode());
					map.put("deviceRunStateName", o.getDeviceRunState().getDeviceRunStateName());
					
					map.put("chkMeansID", o.getChkMeans().getChkMeansID());
					map.put("chkMeansName", o.getChkMeans().getChkMeans());
					
					map.put("chkTypeCode", o.getChkType().getChkTypeCode());
					map.put("chkTypeName", o.getChkType().getChkTypeName());
					list.add(map);
				}
			} 
		} catch(Exception e) {
			e.printStackTrace();
		}
		this.jsonString = this.gson.toJson(list);
		System.out.println(this.jsonString);
		return SUCCESS;
	}
	
	public String load() {
		List list = new ArrayList(); 
		try {
			List<DeviceCheckStandard> deviceCheckStandards = this.deviceCheckStandardService.getDeviceCheckStandard(deviceInfoID);
			for(DeviceCheckStandard o: deviceCheckStandards) {
				Map map = new HashMap();
				map.put("chkStandardID", o.getChkStandardID());
				map.put("chkPlace", o.getChkPlace());
				map.put("deviceInfoID", o.getDevice().getDeviceInfoID());
				map.put("deviceName", o.getDevice().getDeviceName());
				list.add(map);
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		this.jsonString = this.gson.toJson(list);
		System.out.println(this.jsonString);
		return SUCCESS;
	}
	
	public String save() {
		try {
			if(this.isChk!=null&&"on".equals(this.isChk)) {
				this.deviceCheckStandard.setIsCHK(true);
			}
			this.deviceCheckStandardService.addService(this.deviceCheckStandard);
			this.jsonString="{success:true}" ;
		} catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS;
	}
	
	public String delete() {
		try {
			this.deviceCheckStandardService.removeService(this.chkStandardID);
			this.jsonString="{success:true}" ;
		} catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS;
	}
	
	public String update() {
		try {
			this.deviceCheckStandardService.updateService(this.deviceCheckStandard);
			this.jsonString="{success:true}" ;
		} catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS;
	}

	/**
	 * @return the deviceCheckStandardService
	 */
	public DeviceCheckStandardService getDeviceCheckStandardService() {
		return deviceCheckStandardService;
	}

	/**
	 * @param deviceCheckStandardService the deviceCheckStandardService to set
	 */
	public void setDeviceCheckStandardService(
			DeviceCheckStandardService deviceCheckStandardService) {
		this.deviceCheckStandardService = deviceCheckStandardService;
	}

	/**
	 * @return the deviceInfoID
	 */
	public int getDeviceInfoID() {
		return deviceInfoID;
	}

	/**
	 * @param deviceInfoID the deviceInfoID to set
	 */
	public void setDeviceInfoID(int deviceInfoID) {
		this.deviceInfoID = deviceInfoID;
	}

	/**
	 * @return the chkStandardID
	 */
	public int getChkStandardID() {
		return chkStandardID;
	}

	/**
	 * @param chkStandardID the chkStandardID to set
	 */
	public void setChkStandardID(int chkStandardID) {
		this.chkStandardID = chkStandardID;
	}

	/**
	 * @return the deviceCheckStandard
	 */
	public DeviceCheckStandard getDeviceCheckStandard() {
		return deviceCheckStandard;
	}

	/**
	 * @param deviceCheckStandard the deviceCheckStandard to set
	 */
	public void setDeviceCheckStandard(DeviceCheckStandard deviceCheckStandard) {
		this.deviceCheckStandard = deviceCheckStandard;
	}

	/**
	 * @return the nodeDepth
	 */
	public int getNodeDepth() {
		return nodeDepth;
	}

	/**
	 * @param nodeDepth the nodeDepth to set
	 */
	public void setNodeDepth(int nodeDepth) {
		this.nodeDepth = nodeDepth;
	}

	/**
	 * @return the nodeId
	 */
	public String getNodeId() {
		return nodeId;
	}

	/**
	 * @param nodeId the nodeId to set
	 */
	public void setNodeId(String nodeId) {
		this.nodeId = nodeId;
	}

	/**
	 * @return the professService
	 */
	public ProfessService getProfessService() {
		return professService;
	}

	/**
	 * @param professService the professService to set
	 */
	public void setProfessService(ProfessService professService) {
		this.professService = professService;
	}

	/**
	 * @return the isChk
	 */
	public String getIsChk() {
		return isChk;
	}

	/**
	 * @param isChk the isChk to set
	 */
	public void setIsChk(String isChk) {
		this.isChk = isChk;
	}

}
